<%@ page import="cn.kgc.pojo.News" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.io.*,java.util.*,org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../common/common.jsp"%>
<html>
<head>
    <title>doNewsUpdate</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    boolean bRet = false;
    boolean bUpload = false;
    String uploadFileName = "";
    String fieldName = "";
    String picpath2 = "";
    News news = new News();
    news.setModifyDate(new Date());

    //解析请求之前判断请求类型是否为文件上传类型
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    //指定上传位置
    String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/");
    System.out.println("上传路径：" + uploadFilePath);

    File saveDir = new File(uploadFilePath);
    //如果目录不存在，就创建目录
    if (!saveDir.exists()){
        saveDir.mkdir();
    }

    if (isMultipart){
        //创建文件上传核心类
        FileItemFactory factory = new DiskFileItemFactory();//实例化一个硬盘文件工厂，用来配置上传组件ServletFileUpload
        ServletFileUpload upload = new ServletFileUpload(factory); //用以上工厂实例化上传组件
        try {
            //处理表单请求
            List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()){
                FileItem item = (FileItem)iter.next();
                if (item.isFormField()){ //如果是普通表单控件
                    fieldName = item.getFieldName(); //获得该字段名称

                    if (fieldName.equals("id")){
                        news.setId(Integer.parseInt(item.getString()));
                    }else if (fieldName.equals("title")){
                        news.setTitle(item.getString("UTF-8")); //获得字段值
                    }else if (fieldName.equals("categoryId")){
                        news.setCategoryId(Integer.parseInt(item.getString()));
                    }else if (fieldName.equals("summary")){
                        news.setSummary(item.getString("UTF-8"));
                    }else if (fieldName.equals("newscontent")){
                        news.setContent(item.getString("UTF-8"));
                    }else if (fieldName.equals("author")){
                        news.setAuthor(item.getString("UTF-8"));
                    }else if (fieldName.equals("picpath2")){
                        picpath2 = item.getString("UTF-8");
                    }
                }else { //如果为文件域
                    String picFileName = item.getName(); //获得文件名（全路径）
                    if (picFileName != null && !picFileName.equals("")){
                        File fullFile = new File(picFileName);//songzhongji.png
                        System.out.println(fullFile);
                        File saveFile = new File(uploadFilePath,fullFile.getName());//将文件保存到指定的路径 .../upload/songzhongji.png
                        item.write(saveFile);
                        uploadFileName = fullFile.getName();//songzhongji.png
                        news.setPicPath(uploadFileName);
                        bUpload = true;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    System.out.println("上传成功之后的文件名："+news.getPicPath());
    if (news.getPicPath()==null || news.getPicPath().equals("")){
        news.setPicPath(picpath2);
    }
    //调用后台的方法，修改数据库中的新闻信息
    bRet = newsService.updateNews(news);

    if (bRet){
        out.write("修改新闻成功！");
    }else{
        out.write("修改新闻失败！");
    }

%>
<br/><br/><br/><a href="newsDetailList.jsp">>>>返回新闻列表</a>
</body>
</html>
